package com.example.celldian.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBSkill extends SQLiteOpenHelper {

	public DBSkill(Context activity) {
		super(activity, "Celldian.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		String query = "CREATE TABLE skills ( charname TEXT PRIMARY KEY, skillname TEXT, " +
					   "classtype TEXT, type TEXT, damage TEXT, effect TEXT)";
		
		db.execSQL(query);	
		
		Log.i("DATABASE DBSkill:","TABLE skills SUCCESSFULY CREATED!");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String query = "DROP TABLE IF EXISTS skills";
		
		db.execSQL(query);
		onCreate(db);				
		
		Log.i("DATABASE DBSkill:","UPDATE SUCCESSFUL!");
	}
	
	public void insertSkillValue(HashMap<String ,String> skillValues) {
			
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("charname", skillValues.get("charname"));
		values.put("skillname", skillValues.get("skillname"));
		values.put("classtype", skillValues.get("classtype"));
		values.put("type", skillValues.get("type"));
		values.put("damage", skillValues.get("damage"));
		
		db.insert("skills", null, values);
		db.close();
		
		Log.i("DATABASE DBSkill:","SKILL INSERT WAS SUCCESSFUL!");
	}
	
	public int updateSkillValue(HashMap<String ,String> skillValues) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("charname", skillValues.get("charname"));
		values.put("skillname", skillValues.get("skillname"));
		values.put("classtype", skillValues.get("classtype"));
		values.put("type", skillValues.get("type"));
		values.put("damage", skillValues.get("damage"));
		
		Log.i("DATABASE DBSkill:","SKILL UPDATE WAS SUCCESSFUL!");
		
		return db.update("skills", values, "charname" + "?=", new String[] {skillValues.get("charname")});
	}
	
	public void deleteSkillValue(String charname) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		String query = "DELETE FROM skills WHERE charname='" + charname + "'";
		
		db.execSQL(query);
		
		Log.i("DATABASE DBSkill:","SKILL DELETE WAS SUCCESSFUL!");
	}
	
	public ArrayList<HashMap<String, String>> getSkills() {
		
		ArrayList<HashMap<String, String>> skillsList = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM skills";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				HashMap<String, String> skill = new HashMap<String, String>();
				
				skill.put("charname", cursor.getColumnName(0));
				skill.put("skillname", cursor.getColumnName(1));
				skill.put("classtype", cursor.getColumnName(2));
				skill.put("type", cursor.getColumnName(3));
				skill.put("damage", cursor.getColumnName(4));
				
				skillsList.add(skill);
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBSkill:","SKILLS RETURN WAS SUCCESSFUL!");
		
		return skillsList;
	}
	
	public ArrayList<String> getSkill(String charname){
		
		ArrayList<String> skills = new ArrayList<String>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM skills WHERE charname='" + charname + "'";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				skills.add(cursor.getColumnName(1));
				skills.add(cursor.getColumnName(2));
				skills.add(cursor.getColumnName(3));
				skills.add(cursor.getColumnName(4));
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBSkill:","SKILL RETURN WAS SUCCESSFUL!");
		
		return skills;
	}
}
